package com.eascs.cloudFile.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.eascs.admin.common.dao.BaseCrmDao;
import com.eascs.app.context.ConfigContext;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.app.dao.sql.ObjectSqlBuilder;
import com.eascs.cloudFile.entity.CloudDatalist;
import com.eascs.cloudFile.vo.CloudFileVo;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;

/**
 * o2o-crm库DAO
 * 
 * @author user
 *
 */
@Repository
public class CloudFilesDao extends BaseCrmDao {

	public PageResult<Object[]> findCloudFileList(CloudFileVo cloudFileVo){
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<Object[]>(Object[].class, Integer.MAX_VALUE, 0);
		ObjectSqlBuilder objectSqlBuilder = ObjectSqlBuilder.create(cloudFileVo, "");
		sqlPageQuery.setQuerySql(ConfigContext.getStringValue("sql.CloudFilesDao.findCloudFileList") + objectSqlBuilder.getSql() + " ORDER BY c.creat_date ");
		sqlPageQuery.addParams(objectSqlBuilder.getParams());
		return this.findDataWithPage(sqlPageQuery);
	}
	
	public CloudDatalist findCloudFileInfo(String id){
		String sql = " SELECT * FROM cloud_datalist c WHERE c.id = ? ";
		return this.findEntity(sql, CloudDatalist.class, id);
	}
	
	public boolean queryCustomerCloudFile(String customerId){
		String sql = " SELECT COUNT(1) FROM cloud_datalist c WHERE c.customer_id = ? ";
		return this.findInteger(sql, customerId) > 0 ? true : false;
	}
	
	public CloudDatalist findCloudDatalistById(String id) {
		String sql = " select * from cloud_datalist where is_delete = 'N' and id = ? ";
		return this.findEntity(sql, CloudDatalist.class, id);
	}
	
	public List<CloudDatalist> findCloudFilesBySuperAdministrator(String cusomerId, String name) {
		List<CloudDatalist> files = new ArrayList<>();
		StringBuilder sql = new StringBuilder(" SELECT * FROM cloud_datalist c WHERE c.customer_id = ? ");
		if(!StringUtils.isBlank(name)){
			sql.append(" and c.name like ? and c.is_delete = 'N' ORDER BY c.creat_date DESC ");
			files = this.findEntitys(sql.toString(), CloudDatalist.class, cusomerId, "%" + name + "%");
		}else{
			sql.append(" and c.is_delete = 'N' ORDER BY c.creat_date DESC ");
			files = this.findEntitys(sql.toString(), CloudDatalist.class, cusomerId);
		}
		return files;
	}
	
	public List<CloudDatalist> findCloudFiles(String cusomerId, String name, String managerId, String CreateId) {
		List<CloudDatalist> files = new ArrayList<>();
		StringBuilder sql = new StringBuilder(" SELECT * FROM cloud_datalist c WHERE c.customer_id = ? AND c.creat_id IN (?,?) ");
		if(!StringUtils.isBlank(name)){
			sql.append(" and c.name like ? and c.is_delete = 'N' ORDER BY c.creat_date DESC ");
			files = this.findEntitys(sql.toString(), CloudDatalist.class, cusomerId, managerId, CreateId, "%" + name + "%");
		}else{
			sql.append(" and c.is_delete = 'N' ORDER BY c.creat_date DESC ");
			files = this.findEntitys(sql.toString(), CloudDatalist.class, cusomerId, managerId, CreateId);
		}
		return files;
	}
	
	public List<Object[]> getManagerIdAndCreateId(String cusomerId){
		User user = UserHolder.getUser();
		String sql = "SELECT c.credit_manager_id, c.create_id FROM customer_manager c WHERE c.customer_id = ? AND ( c.credit_manager_id = ? OR c.create_id = ? )";
		return  this.findObjectsList(sql, cusomerId, user.getNo(), user.getNo());
	}
}
